<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 证书 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <el-button type="primary" class="handle-del mr10" @click="addDialog">新增证书</el-button>
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAllSelection">批量删除</el-button>
                <el-input v-model="query.name" placeholder="输入证书名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                :data="tableData"
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="name" label="证书名称"></el-table-column>
                <el-table-column prop="time" label="关联考试"></el-table-column>
                <el-table-column prop="time" label="创建时间"></el-table-column>
                <el-table-column label="操作" width="300" align="center">
                    <template slot-scope="scope">
                        <el-row>
                            <el-button @click="preview" type="primary" size="mini">预览</el-button>
                            <el-button @click="edit" type="success" size="mini">编辑</el-button>
                            <el-button @click="deleteRow(scope.$index, scope.row)" type="danger" size="mini">删除</el-button>
                        </el-row>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
        <!-- 新增证书弹出框 -->
        <el-dialog title="新增/编辑" :visible.sync="showDialog" width="30%">
            <el-form ref="form" :model="form" label-width="100px">
                <el-form-item label="证书名称">
                    <el-input v-model="form.name" placeholder="证书名称"></el-input>
                </el-form-item>
                <el-form-item label="关联考试">
                    <el-button @click="innerVisible = true" type="primary" size="small">选择考试</el-button>
                    <div class="choose_user">
                        <div class="user_avatar">
                            <img src="https://wkt.twjha.cn/attachment/images/3/2021/03/WP5kzVY06hPkskKWvY8Q7Q999190Y3_200_200.png" alt="" />
                        </div>
                        <div class="user_nickname">小学精品绘画初级班</div>
                    </div>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 打开内层弹框 -->
        <el-dialog width="36%" title="选择考试" :visible.sync="innerVisible" append-to-body>
            <div class="choice-box">
                <el-input v-model="courseName" placeholder="输入考试名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearchCourse">搜索</el-button>
            </div>
            <div class="course_list" style="height: 300px">
                <div @click="choiceCourse" class="course_list_item" v-for="(item,index) in 9" :key="index">
                    <div class="avatar">
                        <img src="https://wkt.twjha.cn/attachment/images/3/2021/03/WP5kzVY06hPkskKWvY8Q7Q999190Y3_200_200.png" alt="" />
                    </div>
                    <div class="nickname">小学精品绘画初级班</div>
                </div>
            </div>
            <div class="innerPagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="exit">关 闭</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
// import { fetchData } from '../../api/index';
export default {
    name: 'certificate',
    data() {
        return {
            query: {
                pageIndex: 1,
                pageSize: 10
            },
            form: {
                name: '',
                time: '',
            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            showDialog: false,
            innerVisible: false,
            pageTotal: 0,
            courseName: ''
        };
    },
    created() {
        // this.getData();
    },
    methods: {
        getData() {
            fetchData(this.query).then((res) => {
                this.tableData = res.list;
                this.pageTotal = res.pageTotal || 50;
            });
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        //批量删除
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 搜索
        handleSearch() {
            this.$set(this.query, 'pageIndex', 1);
            this.getData();
        },
        handleSearchCourse() {},
        //新增
        addDialog() {
            this.showDialog = true;
        },
        //编辑
        edit() {
            this.showDialog = true;
            this.form.name = '证书';
            this.form.time = '100';
        },
        //删除
        deleteRow(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.$message.success('删除成功');
                    this.tableData.splice(index, 1);
                })
                .catch(() => {});
        },
        choiceCourse() {
            this.innerVisible = false;
        },
        //取消
        cancel() {
            this.showDialog = false;
            this.form = {};
        },
        //确定
        save() {
            this.showDialog = false;
        },
        exit() {
            this.innerVisible = false;
        },
        // 分页
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
    float: right;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}

.mr10 {
    margin-right: 10px;
}
.user_avatar {
    float: left;
    width: 50px;
    height: 50px;
}
.user_avatar img {
    width: 100%;
    border-radius: 50%;
    overflow: hidden;
}
.user_nickname {
    margin-left: 60px;
    line-height: 50px;
}
.choice-box {
    text-align: center;
    margin-bottom: 20px;
}
.course_list_item {
    float: left;
    width: 180px;
    margin: 10px;
    padding: 5px;
    border: 1px solid #eee;
    cursor: pointer;
    height: 62px;
}
.course_list_item .avatar {
    float: left;
    width: 50px;
    height: 50px;
}
.course_list_item .avatar img {
    width: 100%;
    border-radius: 50%;
}
.course_list_item .nickname {
    margin-left: 60px;
    line-height: 28px;
}
.innerPagination {
    float: left;
}
.dialog-footer {
    display: block;
}
</style>


